package com.ifast.data.controller;


import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ifast.common.base.AdminBaseController;
import com.ifast.data.domain.DataDetailsDO;
import com.ifast.data.service.DataDetailsService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 详情页面配置
 * </pre>
 * <small> 2018-04-17 17:21:51 | lvyq</small>
 */
@Controller
@RequestMapping("/data/dataDetails")
public class DataDetailsController extends AdminBaseController {
	@Autowired
	private DataDetailsService dataDetailsService;
	
	@GetMapping()
	@RequiresPermissions("data:dataDetails:dataDetails")
	String DataDetails(){
	    return "data/dataDetails/dataDetails";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataDetails:dataDetails")
	public Result<Page<DataDetailsDO>> list(DataDetailsDO dataDetailsDTO){
        Wrapper<DataDetailsDO> wrapper = new EntityWrapper<DataDetailsDO>(dataDetailsDTO);
        Page<DataDetailsDO> page = dataDetailsService.selectPage(getPage(DataDetailsDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("data:dataDetails:add")
	String add(){
	    return "data/dataDetails/add";
	}

	@GetMapping("/edit/{detailid}")
	@RequiresPermissions("data:dataDetails:edit")
	String edit(@PathVariable("detailid") Long detailid,Model model){
		DataDetailsDO dataDetails = dataDetailsService.selectById(detailid);
		model.addAttribute("dataDetails", dataDetails);
	    return "data/dataDetails/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataDetails:add")
	public Result<String> save( DataDetailsDO dataDetails){
		dataDetailsService.insert(dataDetails);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataDetails:edit")
	public Result<String>  update( DataDetailsDO dataDetails){
		dataDetailsService.updateAllColumnById(dataDetails);
		return Result.ok();
	}
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("data:dataDetails:remove")
	public Result<String>  remove( Long detailid){
		dataDetailsService.deleteById(detailid);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataDetails:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] detailids){
		dataDetailsService.deleteBatchIds(Arrays.asList(detailids));
		return Result.ok();
	}
	
	@GetMapping("/api/v1/getDetails")
	@ResponseBody
	public Result<Map<String,Object>> getDetails(DataDetailsDO dataDetails) {
		Wrapper<DataDetailsDO> wrapper = new EntityWrapper<DataDetailsDO>(dataDetails);
		Map<String,Object> PageinfoMap = dataDetailsService.selectMap(wrapper);
		if(PageinfoMap!=null&&PageinfoMap.size()>0) {
			return Result.ok(PageinfoMap);
		}
		return Result.ok();
	}
	
	@GetMapping("/api/v1/getDetails2")
	@ResponseBody
	public JSONPObject getDetails(String jsonpCallback,DataDetailsDO dataDetails) {
		Wrapper<DataDetailsDO> wrapper = new EntityWrapper<DataDetailsDO>(dataDetails);
		Map<String,Object> dataDetailMap = dataDetailsService.selectMap(wrapper);
		if(dataDetailMap!=null&&dataDetailMap.size()>0) {
			return new JSONPObject(jsonpCallback, Result.ok(dataDetailMap));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}
	
}
